package com.hmeter.mongodb.gui;

import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;

import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.gui.JLabeledTextField;

import com.hmeter.mongodb.MongoDbCreateSampler;

public class MongoDbCreateSamplerGui extends AbstractSamplerGui {

	/**
	 * 
	 */
	private static final long serialVersionUID = 522709729259732877L;
	
	private JLabeledTextField hostTextField = null;
	private JLabeledTextField portTextField = null;
	private JLabeledTextField dbNameTextField = null;
	private JLabeledTextField collectionNameTextField = null;
	private JLabeledTextField keyValuePairsTextField = null;
	private JLabeledTextField jsonContentTextField = null;
	private JLabeledTextField delimiterTextField = null;
	private JCheckBox writeDocumentIntoCollectionCheckBox = null;
	private JCheckBox writeJsonContentIntoCollectionCheckBox = null;
	
	public MongoDbCreateSamplerGui() {
		super();
		init();
	}
	
	public void init() {
		setLayout(new BorderLayout());
        setBorder(makeBorder());
        add(makeTitlePanel(), BorderLayout.NORTH);
        
        hostTextField = new JLabeledTextField("Host");
        portTextField = new JLabeledTextField("Port");
        dbNameTextField = new JLabeledTextField("MongoDB name");
        collectionNameTextField = new JLabeledTextField("Collection name");
        keyValuePairsTextField = new JLabeledTextField("List of key value pairs to write (format: key:value)");
        jsonContentTextField = new JLabeledTextField("JSON content to write");
        delimiterTextField = new JLabeledTextField("Delimiter");
        writeDocumentIntoCollectionCheckBox = new JCheckBox("Write simple key/value list document into collection");
        writeJsonContentIntoCollectionCheckBox = new JCheckBox("Write document into collection taking JSON content as input");
        ButtonGroup options = new ButtonGroup();
        options.add(writeDocumentIntoCollectionCheckBox);
        options.add(writeJsonContentIntoCollectionCheckBox);

        //register events
        changeTextFieldState(writeDocumentIntoCollectionCheckBox, keyValuePairsTextField);
        changeTextFieldState(writeDocumentIntoCollectionCheckBox, delimiterTextField);
        changeTextFieldState(writeJsonContentIntoCollectionCheckBox, jsonContentTextField);
        
        //main panel
        VerticalPanel panel = new VerticalPanel();
        panel.setBorder(BorderFactory.createEtchedBorder());
        
        //input data panel
        VerticalPanel inputPanel = new VerticalPanel();
        inputPanel.setBorder(BorderFactory.createTitledBorder("Input data"));
        inputPanel.add(hostTextField);
        inputPanel.add(portTextField);
        inputPanel.add(dbNameTextField);
        inputPanel.add(collectionNameTextField);
        
        //write document into collection panel
        VerticalPanel writeDocumentIntoCollectionPanel = new VerticalPanel();
        writeDocumentIntoCollectionPanel.setBorder(BorderFactory.createTitledBorder("Write document into collection"));
        writeDocumentIntoCollectionPanel.add(writeDocumentIntoCollectionCheckBox);
        writeDocumentIntoCollectionPanel.add(keyValuePairsTextField);
        writeDocumentIntoCollectionPanel.add(delimiterTextField);

        //write json content into collection panel
        VerticalPanel writeJsonContentIntoCollectionPanel = new VerticalPanel();
        writeJsonContentIntoCollectionPanel.setBorder(BorderFactory.createTitledBorder("Write JSON content into collection"));
        writeJsonContentIntoCollectionPanel.add(writeJsonContentIntoCollectionCheckBox);
        writeJsonContentIntoCollectionPanel.add(jsonContentTextField);
        
        panel.add(inputPanel);
        panel.add(writeDocumentIntoCollectionPanel);
        panel.add(writeJsonContentIntoCollectionPanel);
        
        add(panel, BorderLayout.CENTER);
	}
	
	public void changeTextFieldState(JCheckBox checkBox, final JLabeledTextField textField)
	{
		checkBox.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED)
				{
					textField.setEnabled(true);
				}
				else if (e.getStateChange() == ItemEvent.DESELECTED)
				{
					textField.setEnabled(false);
				}
			}
		}
		);
	}
	
	@Override
	public void clearGui()
	{
		super.clearGui();
		hostTextField.setText("");
		portTextField.setText("");
		dbNameTextField.setText("");
		collectionNameTextField.setText("");
		keyValuePairsTextField.setText("");
		jsonContentTextField.setText("");
		delimiterTextField.setText("");
		writeDocumentIntoCollectionCheckBox.setSelected(true);
		writeJsonContentIntoCollectionCheckBox.setSelected(false);
		keyValuePairsTextField.setEnabled(true);
		jsonContentTextField.setEnabled(false);
		delimiterTextField.setEnabled(true);
	}
	
	@Override
	public String getLabelResource() {
		return getClass().getSimpleName();
	}
	
	@Override
	public String getStaticLabel() {
	    return "MongoDB Create Sampler";
	}
	
	@Override
	public TestElement createTestElement() {
		MongoDbCreateSampler mongoDbCreateSampler = new MongoDbCreateSampler();
		modifyTestElement((TestElement) mongoDbCreateSampler);
		return (TestElement) mongoDbCreateSampler;
	}
	
	@Override
	public void modifyTestElement(TestElement element) {
		super.configureTestElement(element);
		if (element instanceof MongoDbCreateSampler)
		{
			MongoDbCreateSampler mongoDbCreateSampler = (MongoDbCreateSampler) element;
			mongoDbCreateSampler.setHost(hostTextField.getText());
			mongoDbCreateSampler.setPort(portTextField.getText());
			mongoDbCreateSampler.setDbName(dbNameTextField.getText());
			mongoDbCreateSampler.setCollectionName(collectionNameTextField.getText());
			mongoDbCreateSampler.setKeyValuePairs(keyValuePairsTextField.getText());
			mongoDbCreateSampler.setJsonContent(jsonContentTextField.getText());
			mongoDbCreateSampler.setDelimiter(delimiterTextField.getText());
			mongoDbCreateSampler.setWriteDocumentIntoCollectionBool(writeDocumentIntoCollectionCheckBox.isSelected());
			mongoDbCreateSampler.setWriteJsonContentIntoCollectionBool(writeJsonContentIntoCollectionCheckBox.isSelected());
		}
	}
	
	@Override
	public void configure(TestElement element)
	{
		super.configure(element);
		if (element instanceof MongoDbCreateSampler)
		{
			MongoDbCreateSampler mongoDbCreateSampler = (MongoDbCreateSampler) element;
			hostTextField.setText(mongoDbCreateSampler.getHost());
			portTextField.setText(mongoDbCreateSampler.getPort());
			dbNameTextField.setText(mongoDbCreateSampler.getDbName());
			collectionNameTextField.setText(mongoDbCreateSampler.getCollectionName());
			keyValuePairsTextField.setText(mongoDbCreateSampler.getKeyValuePairs());
			jsonContentTextField.setText(mongoDbCreateSampler.getJsonContent());
			delimiterTextField.setText(mongoDbCreateSampler.getDelimiter());
			writeDocumentIntoCollectionCheckBox.setSelected(mongoDbCreateSampler.isWriteDocumentIntoCollectionBool());
			writeJsonContentIntoCollectionCheckBox.setSelected(mongoDbCreateSampler.isWriteJsonContentIntoCollectionBool());
		}
	}
}
